package com.smile.server;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.google.gson.Gson;
import com.smile.constant.RequestType;
import com.smile.db.operation.LoginOperation;
import com.smile.db.operation.RegisterOperation;
import com.smile.interf.ServerViewInterface;
import com.smile.model.ChatSocket;
import com.smile.model.ReceiveString;
import com.smile.model.RegisterBean;
import com.smile.model.SendString;

/**
 * 客户端管理
 * 
 * @author smile
 *
 */
public class ChatManager {
	
	private ServerViewInterface serverViewInterface;
	private Map<String, ChatSocket> client = new HashMap<String, ChatSocket>();
	private static boolean isSend;
	private Gson gson = new Gson();
	
	private ChatManager(){
	}
	private static final ChatManager CM = new ChatManager();
	
	public void setServerViewInterface(ServerViewInterface serverViewInterface){
		this.serverViewInterface = serverViewInterface;
	}
	
	public static ChatManager getCM(){
		return CM;
	}
	
	
	public synchronized void add(ChatSocket cs){
		isSend = true;
		client.put(cs.getClientName(), cs);
		serverViewInterface.setMessage("用户[" + cs.getClientName() + "]上线\n", getCount());
	}
	
	public synchronized void del(ChatSocket cs){
		String name = cs.getClientName();
		client.remove(name);
		if(isSend){
			serverViewInterface.setMessage("用户[" + name + "]下线\n", getCount());
		}
	}
	
	public void delAll(){
		isSend = false;
		Set<String> keys = client.keySet();
		for(String key : keys){
			client.get(key).closeSocket();
		}
		client.clear();
	}
	
	public synchronized int getCount(){
		return client.size();
	}
	
	public synchronized void publish(ChatSocket cs, String out){
		cs.out("服务器已收到消息\n");

		Set<String> keys = client.keySet();
		for(String key : keys){
			if(!client.get(key).equals(cs)){
				client.get(key).out(cs.getClientName() + "说：" + out + "\n");
			}
		}
		System.out.println(out);
	}
	
	public synchronized void dealMessage(ChatSocket cs, String message){
		ReceiveString receive = gson.fromJson(message, ReceiveString.class);
		switch (receive.getType()) {
		case RequestType.LOGIN:
			login(cs, receive.getJson());
			System.out.println("登录---" + receive.getJson());
			break;
		case RequestType.REGISTER:
			register(cs, receive.getJson());
//			System.out.println("注册---" + receive.getJson());
			break;
		case RequestType.SEND:
			System.out.println("发送信息---" + receive.getJson());
			break;
		case RequestType.GETUSER:
			System.out.println("获取用户---" + receive.getJson());
			break;
		case RequestType.GETCHATMESSAGE:
			System.out.println("获取聊天记录---" + receive.getJson());
			break;

		default:
			System.out.println("服务器不接受此请求类型");
			break;
		}
	}
	
	private void register(ChatSocket cs, String json){
		RegisterBean registerBean = gson.fromJson(json, RegisterBean.class);
//		System.out.println(registerBean.toString());
		
		String username = RegisterOperation.getRO().insert(registerBean);
		
		String[] mess = username.split("@");
		SendString sendString = null;
		if(mess[0].equals("-1")){
			switch (mess[1]) {
			case "1062":
				sendString = new SendString(1062, RequestType.REGISTER, "该昵称已存在");
				String json2 = gson.toJson(sendString);
				cs.out(json2 + "\n");
				break;

			default:
				break;
			}
		} else {
			sendString = new SendString(1, RequestType.REGISTER, mess[1]);
			String json2 = gson.toJson(sendString);
			cs.out(json2 + "\n");
		}
	}
	
	private void login(ChatSocket cs, String json){
		RegisterBean registerBean = gson.fromJson(json, RegisterBean.class);
		
		String name = LoginOperation.getLO().login(registerBean);

		String[] mess = name.split("@");
		SendString sendString = null;
		if(mess[0].equals("-1")){
			switch (mess[1]) {
			case "3":
				sendString = new SendString(3, RequestType.REGISTER, "该用户不存在");
				String json2 = gson.toJson(sendString);
				cs.out(json2 + "\n");
				break;
			case "2":
				sendString = new SendString(2, RequestType.REGISTER, "密码错误");
				String json3 = gson.toJson(sendString);
				cs.out(json3 + "\n");
				break;

			default:
				break;
			}
		} else {
			sendString = new SendString(1, RequestType.LOGIN, mess[1]);
			String json2 = gson.toJson(sendString);
			cs.out(json2 + "\n");
		}
		
		
	}
	
}
